package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.search.domain.DestinationEs;
import cn.wolfcode.wolf2w.search.domain.StrategyEs;
import cn.wolfcode.wolf2w.search.domain.TravelEs;
import cn.wolfcode.wolf2w.search.domain.UserInfoEs;
import cn.wolfcode.wolf2w.search.query.SearchQueryObject;
import cn.wolfcode.wolf2w.search.service.*;
import cn.wolfcode.wolf2w.search.vo.SearchHashMap;
import cn.wolfcode.wolf2w.search.vo.SearchResultVo;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping
public class SearchController {

    //es服务
    @Autowired
    private IDestinationEsService destinationEsService;
    @Autowired
    private IStrategyEsService strategyEsService;
    @Autowired
    private ITravelEsService travelEsService;
    @Autowired
    private IUserInfoEsService userInfoEsService;
    @Autowired
    private ISearchService searchService;


    //mongodb服务
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IUserInfoService userInfoService;



    @GetMapping("/dataInit")
    public Object dataInit(){

        //攻略
        List<Strategy> sts = strategyService.findAll();
        for (Strategy st : sts) {
            StrategyEs es = new StrategyEs();
            BeanUtils.copyProperties(st, es);
            strategyEsService.save(es);
        }
        //游记
        List<Travel> ts = travelService.findAll();
        for (Travel t : ts) {
            TravelEs es = new TravelEs();
            BeanUtils.copyProperties(t, es);
            travelEsService.save(es);
        }

        //用户
        List<UserInfo> uf = userInfoService.list();
        for (UserInfo u : uf) {
            UserInfoEs es = new UserInfoEs();
            BeanUtils.copyProperties(u, es);
            userInfoEsService.save(es);
        }


        //目的地
        List<Destination> dests = destinationService.list();
        for (Destination d : dests) {
            DestinationEs es = new DestinationEs();
            BeanUtils.copyProperties(d, es);
            destinationEsService.save(es);
        }

        return "ok";
    }


    @GetMapping("/q")
    public Object search(SearchQueryObject qo) throws UnsupportedEncodingException {
        String decode = URLDecoder.decode(qo.getKeyword(), "UTF-8");
        qo.setKeyword(decode);


        switch (qo.getType()){
            case -1 :   return this.allSearch(qo);
            case  0  :  return this.destinationEsSearch(qo) ;
            case  1  :  return this.StrategySearch(qo) ;
            case  2  :  return this.travelEsSearch(qo) ;
            default :   return this.userInfoEsSearch(qo);
        }
    }

    public Object destinationEsSearch(SearchQueryObject qo){

        //查询目的地
        Destination destination = destinationService.findByName(qo.getKeyword());
        SearchResultVo searchResultVo = new SearchResultVo();
        //SearchHashMap searchVo = new SearchHashMap();
        if (destination != null){
            //查询攻略
            List<Strategy> strategies = strategyService.findByDestName(qo.getKeyword());
            //查询游记
            List<Travel> travels = travelService.findByDestId(destination.getId());
            //查用户
            List<UserInfo> userInfos = userInfoService.findByDestName(qo.getKeyword());

            searchResultVo.setStrategys(strategies);
            searchResultVo.setTravels(travels);
            searchResultVo.setUsers(userInfos);
            searchResultVo.setTotal(strategies.size()+travels.size()+userInfos.size() + 0L);
            //searchVo.put("strategys", strategies).put("users", userInfos).put("travels", travels).put("total",strategies.size()+travels.size()+userInfos.size() );
        }


        HashMap<String, Object> map = new HashMap<>();
        map.put("qo", qo);
        map.put("result",searchResultVo);
        map.put("dest",destination);

        return JsonResult.success(map);
    }

    public Object travelEsSearch(SearchQueryObject qo){
        Page<Travel> page = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME, Travel.class, qo,
                "title", "summary");

        return JsonResult.success(new SearchHashMap().put("page", page).put("qo", qo));
    }

    //攻略
    private Object StrategySearch(SearchQueryObject qo) {
        Page<Strategy> page = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class, qo,
                "title", "subTitle", "summary");
        return JsonResult.success(new SearchHashMap().put("page", page).put("qo", qo));
    }

    public Object userInfoEsSearch(SearchQueryObject qo){
        Page<UserInfo> page = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class, qo,
                "city", "info");
        return JsonResult.success(new SearchHashMap().put("page", page).put("qo", qo));
    }

    public Object allSearch(SearchQueryObject qo){

        Page<Strategy> sts = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class, qo,
                "title", "subTitle", "summary");
        Page<Travel> ts = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME, Travel.class, qo,
                "title", "summary");
        Page<UserInfo> us = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class, qo,
                "city", "info");
        Page<Destination> ds = searchService.searchWithHighlight(DestinationEs.INDEX_NAME, DestinationEs.TYPE_NAME, Destination.class, qo,
                "name", "info");


        SearchResultVo vo = new SearchResultVo();

        vo.setDests(ds.getContent());
        vo.setStrategys(sts.getContent());
        vo.setTravels(ts.getContent());
        vo.setUsers(us.getContent());

        vo.setTotal(sts.getTotalElements() + ts.getNumberOfElements() + us.getTotalElements() + ds.getTotalElements());

        return JsonResult.success(new SearchHashMap().put("result", vo).put("qo", qo));
    }
}
